package com.caowj.composedemo.composable

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.caowj.composedemo.R
import com.caowj.composedemo.widget.KeyNavigationRoute

/**
 * <pre>
 *     学习页面
 *     作者：Caowj
 *     邮箱：caoweijian@caowj.com
 *     日期：2022/3/21 16:53
 * </pre>
 */
@Composable
fun LearnCompose(navHostController: NavHostController) {
    _contentPage(navHostController)
}


@Composable
private fun _contentPage(navHostController: NavHostController) {
    val itemList = mutableListOf<ItemInfo2>()
    itemList.add(ItemInfo2("手势学习", KeyNavigationRoute.GESTURES))
    itemList.add(ItemInfo2("State状态管理", KeyNavigationRoute.STATE))

    LazyColumn(
        modifier = Modifier.padding(horizontal = 5.dp, vertical = 5.dp),
        //为每行添加间隔
        verticalArrangement = Arrangement.spacedBy(5.dp)
    ) {
        itemsIndexed(items = itemList) { index, item -> //遍历内容和索引
//            Text(text = "索引:$index -- 内容:$item")
            _IntentButton(navHostController, item, index)
        }
    }
}


@Composable
private fun _IntentButton(navHostController: NavHostController, itemInfo: ItemInfo2, index: Int) {
    Button(
        onClick = {
            navHostController.navigate(itemInfo.keyRoute.routeStr)
        },
        modifier = Modifier.fillMaxWidth(),
        shape = RoundedCornerShape(5.dp),
        contentPadding = PaddingValues(15.dp),
        colors = androidx.compose.material.ButtonDefaults.buttonColors(
            backgroundColor = Color(R.color.teal_700),
            contentColor = Color.LightGray,
        )
    ) {
        Text(text = "${index + 1}、${itemInfo.titleStr}")
    }
}

data class ItemInfo2(val titleStr: String, val keyRoute: KeyNavigationRoute)